package com.sgb.kjwl.viewmodel.category;

import android.annotation.SuppressLint;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.repository.CategoryRepertory;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.kjwl.view.ui.fragment.CategoryFragment4;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.MenuEntity;

import java.lang.reflect.Type;
import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @author Administrator
 */
public class CategoryFragModel4 extends BaseViewModel {
    private CategoryRepertory repertory;
    private CategoryFragment4 fragment;

    public CategoryFragModel4(CategoryFragment4 fragment, CategoryRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getCategoryModel(String name) {
        if ("租赁".equals(name)) {
            getLeaseCategoryModel();
        } else {
            getCategoryModel();
        }
    }

    private void getCategoryModel() {
        Type type = new TypeToken<List<MenuEntity<MenuEntity<MenuEntity>>>>() {
        }.getType();
        Object object = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CachegoryModel, type);
        if (null != object) {
            List<MenuEntity<MenuEntity<MenuEntity>>> cacheList = (List<MenuEntity<MenuEntity<MenuEntity>>>) object;
            if (cacheList.size() > 0) {
                fragment.showCategoryView(cacheList);
                return;
            }
        }
        repertory.getCategoryData(repertory.getAreaCode())
                .subscribe(new Consumer<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
                    @SuppressLint("CheckResult")
                    @Override
                    public void accept(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getData() != null && listBaseEntity.getData().size() > 0) {
                            //获取缓存哈希值
                            String mHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.CachegoryModel);
                            //获取从网络请求下来的数据的哈希值
                            String mHashNet = DatalUtils.getInstance().strToHashKey(listBaseEntity.getData());
                            if (!mHash.equals(mHashNet)) {
                                fragment.showCategoryView(listBaseEntity.getData());
                                DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.CachegoryModel, listBaseEntity.getData());
                            }
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    /**
     * 获取租赁全部分类
     */
    public void getLeaseCategoryModel() {
        Type type = new TypeToken<List<MenuEntity<MenuEntity<MenuEntity>>>>() {
        }.getType();
        Object object = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CacheGoryLeaseModel, type);
        if (null != object) {
            List<MenuEntity<MenuEntity<MenuEntity>>> cacheList = (List<MenuEntity<MenuEntity<MenuEntity>>>) object;
            if (cacheList.size() > 0) {
                fragment.showCategoryView(cacheList);
            }
        }

        APIManager.getInstance().getLeaseAPI().getLeaseAllCategoryList().enqueue(new Callback<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> call, Response<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    //获取缓存哈希值
                    String mHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.CacheGoryLeaseModel);
                    //获取从网络请求下来的数据的哈希值
                    String mHashNet = DatalUtils.getInstance().strToHashKey(response.body().getData());
                    if (!mHash.equals(mHashNet)) {
                        fragment.showCategoryView(response.body().getData());
                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.CacheGoryLeaseModel, response.body().getData());
                    }

                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, t.getMessage());
            }
        });
    }

    public void recommendMenu() {

        /*List<MenuEntity> menuEntityList = null;
        //获取缓存数据
        List<MenuEntity> mMenuEntityList = DatalUtils.getCaCheData(CacheCateGory.CatcheRecommendMenu, menuEntityList);
        //获取缓存哈希值
        String mHash = DatalUtils.getHashData(CacheCateGory.CatcheRecommendMenu);
        //返回页面数据
        fragment.setData(mMenuEntityList);
*/
        APIManager.getInstance().getMaterialAPI().recommendMenuNew().enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                if (response == null)
                    return;
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {

                    fragment.setData(response.body().getData());

                    /*Gson gson = new Gson();
                    List<MenuEntity> data1 = response.body().getData();
                    String data = gson.toJson(response.body().getData());
                    //获取从网络请求下来的数据的哈希值
                    String mHashNet = DatalUtils.strToHashKey(data);
                    //做以对比如果不相等的话直接把新数据替换
                    if (!mHash.equals(mHashNet)) {
                        fragment.setData(response.body().getData());
                        Mkvtils.get().putString(CacheCateGory.CatcheRecommendMenu, data);
                    }*/

                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    public void getIndustryId(String firstCategoryId, String secondCategoryId, String thirdCategoryId, int position){
        NewAPIManager.getInstance().getGoodsAPI().getIndustryid(firstCategoryId).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if(response.body().getData()!=null) {
                        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(response.body().getData()));
                        if (json.get("id") != null) {
                            fragment.startForResemble(firstCategoryId, secondCategoryId, thirdCategoryId, position, json.getLong("id") + "");
                        } else {
                            fragment.startForResemble(firstCategoryId, secondCategoryId, thirdCategoryId, position, "-1");
                        }
                    }else{
                        fragment.startForResemble(firstCategoryId, secondCategoryId, thirdCategoryId, position, "-1");
                    }

                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

}
